/**
 * Program : WriteFileUtils.java 
 * Author : shadow guo 
 * Create : 2009-11-1 上午01:13:36
 * 
 * Copyright 2008 by Tongfu Information Technology Co. Ltd All rights reserved.
 * 
 * This software is the confidential and proprietary information of Tongfu Information Technology
 * Co. Ltd. ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with
 * Tongfu Information Technology Co. Ltd.
 * 
 */

/**
 * 
 */
package com.tongfusoft.utils.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/**
 * @author shadowguo
 * 
 */
public class WriteFileUtils {
  private static String encoding = System.getProperties().getProperty("file.encoding");

  public WriteFileUtils() {
    super();
  }

  /**
   * 
   * 写入文本型文件
   * 
   * @param fileName
   * @param content
   * @return void
   * @throws
   */
  public static void writeFile(String fileName, String content)
      throws UnsupportedEncodingException, IOException {
    writeFile(fileName, content, encoding);
  }

  /**
   * 
   * 指定编码格式，将文本内容编码后写入文件
   * 
   * @param fileName
   * @param content
   * @param encoding
   * @return void
   * @throws
   */
  public static void writeFile(String fileName, String content, String encoding)
      throws UnsupportedEncodingException, IOException {
    writeFile(fileName, content.getBytes(encoding));
  }

  /**
   * 
   * 将文本内容写入文件，可以设定是否添加在原文件末尾或覆盖
   * 
   * @param fileName
   * @param append
   * @param content
   * @return void
   * @throws
   */
  public static void writeFile(String fileName, boolean append, String content)
      throws UnsupportedEncodingException, IOException {
    writeFile(fileName, append, content, encoding);
  }

  /**
   * 
   * 指定编码格式，将文本内容编码后写入文件，可以设定是否添加在原文件末尾或覆盖
   * 
   * @param fileName
   * @param append
   * @param content
   * @param encoding
   * @return void
   * @throws
   */
  public static void writeFile(String fileName, boolean append, String content, String encoding)
      throws UnsupportedEncodingException, IOException {
    writeFile(fileName, append, content.getBytes(encoding));
  }

  /**
   * 
   * 写入字节型文件
   * 
   * @param fileName
   * @param bs
   * @return void
   * @throws
   */
  public static void writeFile(String fileName, byte[] bs) throws IOException {
    writeFile(fileName, false, bs);
  }

  /**
   * 
   * 写入字节型文件，可以设定是否添加在原文件末尾或覆盖 (这里描述这个方法适用条件 – 可选) (这里描述这个方法的执行流程 – 可选) (这里描述这个方法的使用方法 – 可选)
   * (这里描述这个方法的注意事项 – 可选)
   * 
   * @param
   * @return void
   * @throws
   */
  public static void writeFile(String fileName, boolean append, byte[] bs) throws IOException {
    writeFile(new FileOutputStream(fileName, append), bs);
  }

  /**
   * 向输出流中写入内容（字符）
   * 
   * @param out
   * @param content
   * @return void
   * @throws
   */
  public static void writeFile(OutputStream out, String content)
      throws UnsupportedEncodingException, IOException {
    writeFile(out, content, encoding);
  }

  /**
   * 
   * 安装指定的编码格式，向输出流中写入内容
   * 
   * @param
   * @return void
   * @throws
   */
  public static void writeFile(OutputStream out, String content, String encoding)
      throws UnsupportedEncodingException, IOException {
    writeFile(out, content.getBytes(encoding));
  }

  /**
   * 
   * 向输出流中写入内容（字节）
   * 
   * @param
   * @return void
   * @throws
   */
  public static void writeFile(OutputStream out, byte[] bs) throws IOException {
    try {
      out.write(bs);
      out.flush();
    } finally {
      out.close();
    }
  }

  /**
   * 
   * 向字符输出流中写入字符内容,使用系统默认编码
   * 
   * @param out
   * @param content
   * @return void
   * @throws
   */
  public static void writeFile(Writer out, String content) throws UnsupportedEncodingException,
      IOException {
    writeFile(out, content, encoding);
  }

  /**
   * 
   * 使用指定编码，向字符输出流中写入字符内容。
   * 
   * @param out
   * @param content
   * @param encoding
   * @return void
   * @throws
   */
  public static void writeFile(Writer out, String content, String encoding)
      throws UnsupportedEncodingException, IOException {
    writeFile(out, new String(content.getBytes(encoding), encoding).toCharArray());
  }

  /**
   * 
   * 向字符流中输出字符数组
   * 
   * @param out
   * @param cs
   * @return void
   * @throws
   */
  public static void writeFile(Writer out, char[] cs) throws IOException {
    try {
      out.write(cs);
      out.flush();
    } finally {
      out.close();
    }
  }

  public static void main(String args[]) {
    WriteFileUtils test = new WriteFileUtils();
    String temp = "中文测试";

    try {
      test.writeFile("d:\\a.txt", temp);
      test.writeFile("d:\\b.txt", temp, "UTF-8");

    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}
